#import "UnityAppController.h"
#import <AVFoundation/AVFoundation.h>

@interface AudioStudioAppController : UnityAppController

@end

IMPL_APP_CONTROLLER_SUBCLASS (AudioStudioAppController)

@implementation AudioStudioAppController
    -(void) AddRouteChangeListener
    {
        [[AVAudioSession sharedInstance]
                setActive:YES
                error:nil];//创建单例对象并且使其设置为活跃状态.
        [[NSNotificationCenter defaultCenter]
                addObserver:self
                selector:@selector(audioRouteChangeListenerCallback:)
                name:AVAudioSessionRouteChangeNotification
                object:nil];
                //object:[AVAudioSession sharedInstance]];
    }

    -(void) audioRouteChangeListenerCallback: (NSNotification*)notification
    {
        NSDictionary *interuptionDict = notification.userInfo;
        NSInteger routeChangeReason   = [[interuptionDict valueForKey:AVAudioSessionRouteChangeReasonKey] integerValue];
    
        switch (routeChangeReason) {
            case AVAudioSessionRouteChangeReasonNewDeviceAvailable:
                NSLog(@"AVAudioSessionRouteChangeReasonNewDeviceAvailable");
                //插入耳机
                UnitySendMessage("AudioManager", "OnWiredHeadPhoneConnected", "");
                break;
            case AVAudioSessionRouteChangeReasonOldDeviceUnavailable:
                NSLog(@"AVAudioSessionRouteChangeReasonOldDeviceUnavailable");
                //拔出耳机
                if ([self isHeadPhoneConnected])
                {
                    UnitySendMessage("AudioManager", "OnWiredHeadPhoneConnected", "");
                }
                else
                {
                    UnitySendMessage("AudioManager", "OnWiredHeadPhoneDisConnected", "");
                }
                break;
        }
    }

    - (void)applicationWillTerminate:(UIApplication*)application
    {
        [[NSNotificationCenter defaultCenter]
                removeObserver:self];
        [super applicationWillTerminate:application];
    }

    - (bool)isHeadPhoneConnected
    {
        AVAudioSessionRouteDescription* route = [[AVAudioSession sharedInstance] currentRoute];
        
        for (AVAudioSessionPortDescription* desc in [route outputs]) {
            if ([[desc portType] isEqualToString:AVAudioSessionPortHeadphones])
                return true;
            if ([[desc portType] isEqualToString:AVAudioSessionPortBluetoothHFP])
                return true;
            if ([[desc portType] isEqualToString:AVAudioSessionPortBluetoothA2DP])
                return true;
        }
        
        return false;
    }
@end

extern "C" 
{
    AudioStudioAppController* hpd = nil;

    bool _IsHeadPhoneConnected() {
        bool bRet = false;
        if (hpd != nil)
        {
            bRet = [hpd isHeadPhoneConnected];
        }
        
        return bRet;
    }

    void _AddRouteChangeListener(){
        hpd = [[AudioStudioAppController alloc] init];
        [hpd AddRouteChangeListener];
    }
}

    